<template>
	<view class="knmnknmert">
		<view class="row">
			<view class="jhhbsrrt fz28 z9 pr" :class="ikdx==idx?'act':''" v-for="(sd,idx) in jhhnsd" :key="idx" @tap="qiehuans(idx)">
				<view class="f_b pr">
					{{sd}}
					<text class="xiahuaxian"></text>
				</view>
			</view>
		</view>

		<view class="" v-if="ikdx==0">
			<view class="mt120">
				<input type="number" value="" placeholder="手机号" maxlength="11" class="fz30 beertert" @input="knnkjnst" v-model="form.tel" @change="checkmo"/>
				<input type="text" password value="" placeholder="密码" class="fz30 beertert mt40" v-model="form.paw" @input="knnkjnst"/>				
				<navigator class="z9 fz24 tr mt20" url="retrievePassword">
					忘记密码
				</navigator>
			</view>
			<view class="mt60 hjjnnxeert cf cen" :class="isjz?'act':''" @tap="dengljse">
				登 录
			</view>
		</view>
		<view class="" v-if="ikdx==1">
			<view class="mt120">
				<view class="pr">
					<input type="number" value="" placeholder="手机号" class="fz30 beertert" @input="knnkjnster" v-model="former.tel"  maxlength="11"/>
					<view class="ls fz24 kjnxeeetxe" @tap="cjjseerr">
						<text v-if="daojishi>0" class="z9">{{daojishi}}s重新获取</text>
						<text v-else>获取验证码</text>
					</view>
				</view>
				<input type="number" password value="" placeholder="验证码" class="fz30 beertert mt40" v-model="former.yzm" @input="knnkjnster" />
			</view>
			<view class="mt110 hjjnnxeert cf cen" :class="isjzer?'act':''" @tap="dengljse">
				登 录
			</view>
		</view>
		<view class="dishabgvs row">
			<view class="col">
				<view class="f_b jkjnneete">
					<image src="../../static/img/sdfsdfa.png" mode="widthFix" class="jjnmxexeer cz"></image>
				</view>
			</view>
			<!-- <view class="col">
				<view class="f_b jkjnneete">
					<image src="../../static/img/sdfsdfb.png" mode="widthFix" class="jjnmxexeer cz"></image>
				</view>
			</view> -->
		</view>	
			
	</view>
</template>
<script>
	var jsModule = uni.requireNativePlugin("MyModule")
	export default {
		data() {
			return {
				ikdx: 0,
				jhhnsd: [
					'账号密码登录',
					'验证码登录'
				],
				daojishi: 0,
				verification_key: '',
				form: {
					tel: '',
					paw: ''
				},
				former: {
					tel: '',
					yzm: ''
				},
				isjz: true,
				isjzer: true
			}
		},
		components: {},
		methods: {
			daojishise() {
				let hhhbgde = setInterval(a => {
					if (this.daojishi-- < 1) {
						clearInterval(hhhbgde)
					}
				}, 1000)
			},
			cjjseerr() {
				if (this.daojishi > 0) {
					return
				}
				if (!this.checkMobile(this.former.tel)) {
						uni.showToast({
							icon: "none",
							title: '请输入正确的手机号'
						})
						return
				}
				this.fasseddert()
				this.daojishi = 60
				this.daojishise()
			},
			async fasseddert() {
				if (!this.checkMobile(this.former.tel)) {
					uni.showToast({
						icon: "none",
						title: '请输入正确的手机号'
					})
					return
				}
				let cs = {}
				cs.phone = this.former.tel
				let jbhss = await this.post('/api/verificationCodes', cs)
				this.verification_key = jbhss.key
			},
			checkMobile(tel){
				console.log(tel);
			    var reg =/^0?1[3|4|5|6|7|8][0-9]\d{8}$/;
			    return reg.test(tel);
			},
			qiehuans(idx) {
				this.ikdx = idx
			},
			knnkjnst() {
				this.isjz = true
				if (!this.form.tel) {
					return
				}
				if (!this.form.paw) {
					return
				}
				this.isjz = false
			},
			knnkjnster() {
				this.isjzer = true
				console.log(this.former)
				if (!this.former.tel) {
					return
				}
				if (!this.former.yzm) {
					return
				}
				this.isjzer = false
			},
			async dengljse() {
				let cs = {}
				cs.phone = this.form.tel
				if (this.ikdx == 0) {
					cs.password = this.form.paw
				}
				if (this.ikdx == 1) {
					cs.verification_key = this.verification_key
					cs.verification_code = this.former.yzm
				}
				try {
					const res = uni.getSystemInfoSync();
					cs.device = res.model
				} catch (e) {
					cs.device = '华为'
				}
				let jhbsw = parseInt(this.ikdx)+1
				let jhsd = await this.post("/api/authorizations/" + jhbsw  , cs)
				uni.setStorageSync('access_token',jhsd.token_type+jhsd.access_token)
				uni.setStorageSync('uid',jhsd.uid)
				uni.setStorageSync('uinfo',jhsd)
				let sdee = new Date().getTime()
				uni.setStorageSync('times', sdee)
				uni.setStorageSync('expires_in', jhsd.expires_in)
				let userinfo = await this.get('/api/user',{})
				this.res = userinfo.data
				this.$store.commit('setuserinfo', userinfo.data)
				uni.switchTab({
					url:"/pages/zhongjianbtn/index"
				})
			}
		},
		mounted() {}
	}
</script>
<style scoped>
</style>
